﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace GzipManagerPro.UI.Utility
{
    [RunInstaller(true)]
    public partial class ContextMenuInstaller : System.Configuration.Install.Installer
    {
        public ContextMenuInstaller()
        {
            InitializeComponent();
        }

        protected override void OnCommitted(IDictionary savedState)
        {
            base.OnCommitted(savedState);

            RegisterContextMenu();
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

            UnRegisterContextMenu();
        }

        private const string IconFileName = "Archive.ico";
        private const string FolderKeyMenuName = "Folder\\shell\\GZipMPro";
        public const string FolderKeyCommand = "Folder\\shell\\GZipMPro\\command";

        private const string FileKeyMenuName = "*\\shell\\GZipMPro";
        public const string FileKeyCommand = "*\\shell\\GZipMPro\\command";

        const string MenuName = "Add To GZip Manager Pro Archive";

        public static void RegisterContextMenu()
        {
            RegistryKey regfoldermenu = null;
            RegistryKey regfoldercmd = null;
            RegistryKey regfilemenu = null;
            RegistryKey regfilecmd = null;

            string command = string.Format("\"{0}\" -a \"%1\"", System.Reflection.Assembly.GetExecutingAssembly().Location);
            try
            {
                // find application logo address
                string iconPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

                // If the icon is located at the same place as the whole application
                if (File.Exists(Path.Combine(iconPath, IconFileName)))
                    // We are on deployed state.
                    iconPath = Path.Combine(iconPath, IconFileName);
                else
                    // We are on development state, so using the resources folder
                    iconPath = Path.Combine(iconPath, "..", "..", "Resources", IconFileName);

                // Add menu item "Add to Gzip Manager Pro" in folder right click menu
                regfoldermenu = Registry.ClassesRoot.CreateSubKey(FolderKeyMenuName);
                if (regfoldermenu != null)
                {
                    regfoldermenu.SetValue("", MenuName);
                    regfoldermenu.SetValue("Icon", iconPath);
                }
                regfoldercmd = Registry.ClassesRoot.CreateSubKey(FolderKeyCommand);
                if (regfoldercmd != null)
                    regfoldercmd.SetValue("", command);

                // Add menu item "Add to Gzip Manager Pro" in file right click menu
                regfilemenu = Registry.ClassesRoot.CreateSubKey(FileKeyMenuName);
                if (regfilemenu != null)
                {
                    regfilemenu.SetValue("", MenuName);
                    regfilemenu.SetValue("Icon", iconPath);
                }
                regfilecmd = Registry.ClassesRoot.CreateSubKey(FileKeyCommand);
                if (regfilecmd != null)
                    regfilecmd.SetValue("", command);
            }
            catch
            {

            }
            finally
            {
                if (regfoldermenu != null)
                    regfoldermenu.Close();
                if (regfoldercmd != null)
                    regfoldercmd.Close();

                if (regfilemenu != null)
                    regfilemenu.Close();
                if (regfilecmd != null)
                    regfilecmd.Close();
            }


        }

        public static void UnRegisterContextMenu()
        {
            try
            {
                RegistryKey regFolder = Registry.ClassesRoot.OpenSubKey(FolderKeyCommand);
                if (regFolder != null)
                {
                    regFolder.Close();
                    Registry.ClassesRoot.DeleteSubKey(FolderKeyCommand);
                }
                regFolder = Registry.ClassesRoot.OpenSubKey(FolderKeyMenuName);
                if (regFolder != null)
                {
                    regFolder.Close();
                    Registry.ClassesRoot.DeleteSubKey(FolderKeyMenuName);
                }

                RegistryKey regFile = Registry.ClassesRoot.OpenSubKey(FileKeyCommand);
                if (regFile != null)
                {
                    regFile.Close();
                    Registry.ClassesRoot.DeleteSubKey(FileKeyCommand);
                }
                regFile = Registry.ClassesRoot.OpenSubKey(FileKeyMenuName);
                if (regFile != null)
                {
                    regFile.Close();
                    Registry.ClassesRoot.DeleteSubKey(FileKeyMenuName);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
            }
        }
    }
}
